<template>
  <keep-alive style="overflow: auto">
    <!-- 列表形式显示文件 -->

    <ul v-if="this.$store.state.changeShow.showWay == 'list'">
      <!-- v-for循环获取的文件 -->
      <li
        class="listItems"
        :class="{ ' bgOver': display == item.ID + 1 }"
        @mouseover="mouseOverItem(item.ID + 1)"
        @mouseout="mouseOutItem()"
        v-for="item in this.$store.state.documents.get.getDocument"
        :key="item.ID"
        @click="openFolder(item.IsDir, item.Path)"
      >
        <img :src="item.MimeType" alt="" />
        <div class="message">
          <h5 class="move">{{ item.Name }}</h5>
          <p class="move">
            {{ item.IsDir ? "文件夹" : "文件" }}
            {{
              item.IsRegular
                ? item.Size > 1024
                  ? item.Size / 1024 > 1024
                    ? item.Size / 1024 / 1024 > 1024
                      ? item.Size / 1024 / 1024 / 1024 > 1024
                        ? (item.Size / 1024 / 1024 / 1024 / 1024).toFixed(2) /
                            1 +
                          "TB"
                        : (item.Size / 1024 / 1024 / 1024).toFixed(2) / 1 + "GB"
                      : (item.Size / 1024 / 1024).toFixed(2) / 1 + "MB"
                    : (item.Size / 1024).toFixed(2) / 1 + "KB"
                  : item.Size.toFixed(2) / 1 + "B"
                : ""
            }}
          </p>
        </div>
        <!-- 鼠标划入按钮出现 -->
        <div
          class="mouse_Over_Btn"
          @click.stop="doSomething($event)"
          :class="{ ' mouseOver': display }"
        >
          <div
            class="btn"
            :class="{ ' allowGetFolder': item.IsDir && !allowGetFolder }"
            @click="download(item.IsDir, item.Path)"
          >
            下载
          </div>
          <div class="btn" style="display: none">删除</div>
        </div>
      </li>
    </ul>
    <!-- 网状形式显示文件 -->
    <ul class="grid" v-else>
      <li
        class="gridItems"
        :class="{ ' bgOver': display == item.ID + 1 }"
        v-for="item in this.$store.state.documents.get.getDocument"
        :key="item.ID"
        @click="openFolder(item.IsDir, item.Path)"
        @mouseover="mouseOverItem(item.ID + 1)"
        @mouseout="mouseOutItem()"
      >
        <div>
          <img
            :src="item.MimeType"
            alt=""
            :class="{ ' hiddenImg': display == item.ID + 1 }"
          />
          <div
            @click.stop="doSomething($event)"
            class="mouse_Over_Btn"
            :class="{ ' mouseShow': display == item.ID + 1 }"
          >
            <div
              class="btn"
              :class="{ ' allowGetFolder': item.IsDir && !allowGetFolder }"
              @click="download(item.IsDir, item.Path)"
            >
              下载
            </div>
            <div class="btn">删除</div>
          </div>
          <h5>{{ item.Name }}</h5>
        </div>
      </li>
    </ul>
  </keep-alive>
</template>


<script>
import axios from "axios";
export default {
  name: "itemComponent",
  data() {
    return {
      getDocument: [],
      display: "",
      path: "",
      allowUpload: false,
      allowGetFolder: false,
    };
  },

  methods: {
    //鼠标划入事件
    mouseOverItem(i) {
      this.display = i;
    },
    //鼠标画出事件
    mouseOutItem() {
      this.display = false;
    },

    //点击下载按钮，下载该文件或者文件夹
    download(dir, path) {
      if (dir) {
        this.$store.dispatch("downloadFolderPath", path);
        this.path = this.$store.state.downloadFolderPath.path;
        this.$message({
          message: "该文件夹即将下载，请稍后......",
          duration: 800,
          onClose: () => {
            window.open(this.path);
          },
        });
      } else {
        this.$store.dispatch("downloadFilePath", path);
        this.path = this.$store.state.downloadFilePath.path;
        this.$message({
          message: "该文件即将下载，请稍后......",
          duration: 800,
          onClose: () => {
            window.open(this.path);
          },
        });
      }
    },
    //打开访问该文件夹
    openFolder(IsDir, Path) {
      if (IsDir) {
        console.log(Path);
        this.$store.dispatch("openFolder", Path);
      } else {
        return;
      }
    },
  },
  async created() {
    await this.$store.dispatch("fetch");
  },
  mounted() {
    axios.get("/config").then((res) => {
      this.allowUpload = res.data.AllowUpload;
      this.allowGetFolder = res.data.AllowGetFolder;
    });
  },
};
</script>

<style lang="less" scoped>
//黑夜模式
@media screen and (prefers-color-scheme: dark) {
  ul /deep/ .bgOver {
    background-color: #808080;
  }
}
// 列表显示
.listItems {
  position: relative;
  display: flex;
  width: 100%;
  height: 91px;
  //文件图标
  img {
    margin: 16px 0 16px 31px;
    width: 59px;
    height: 59px;
    order: 0;
  }
  //文件信息
  .message {
    margin: 19px 8px 0 22px;
    h5 {
      font-size: 18px;
    }
    p {
      font-size: 18px;
      color: rgba(162, 162, 162, 100);
    }
  }
  //文件上去出现按钮
  .mouse_Over_Btn {
    display: none;
    // display: flex;
    position: absolute;
    right: 35px;
    .btn {
      margin-top: 31px;
      margin-right: 12px;
      width: 80px;
      height: 30px;
      line-height: 30px;
      border-radius: 4px;
      background-color: rgba(164, 173, 179, 100);
      color: #fff;
      font-size: 14px;
      text-align: center;
      font-family: Microsoft Yahei;
      cursor: pointer;
    }
  }
}
//文件网格显示
.grid {
  display: flex;
  padding: 12px;
  flex-wrap: wrap;
  justify-self: start;
  .gridItems {
    margin: 10px 10px 10px 10px;
    width: 120px;
    height: 120px;
    overflow: hidden;
    img {
      // display: none;
      margin: 10px 30px 10px 30px;
      width: 60px;
      height: 60px;
    }
    //鼠标到item上图片消失
    .hiddenImg {
      display: none;
    }
    h5 {
      text-align: center;
    }
    //文件上去出现按钮
    .mouse_Over_Btn {
      display: none;
      margin: 10px auto;
      width: 80px;
      height: 60px;
      display: none;
      // display: flex;
      // flex-flow: column;
      .btn {
        width: 80px;
        height: 27px;
        margin: 3px 0 3px 0;
        line-height: 25px;
        border-radius: 4px;
        background-color: rgba(164, 173, 179, 100);
        color: #fff;
        font-size: 14px;
        text-align: center;
        font-family: Microsoft Yahei;
        cursor: pointer;
      }
    }
    .mouseShow {
      display: flex;
      flex-flow: column;
    }
  }
}
//控制背景颜色改变
.bgOver {
  background-color: #f2f2f2;
  //控制按钮出现
  .mouseOver {
    display: flex;
  }
}

//根据服务器传来的权限，控制下载开关是否出现
.allowGetFolder {
  display: none;
}
//移动到文字上，鼠标变成手的样子
.move {
  cursor: pointer;
}
</style>